using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Timer;
using MicroMsg.Common.Utils;
using System;
namespace MicroMsg.Scene.Voice
{
	public class UploadVoiceService
	{
		private const string TAG = "UploadVoiceService";
		private const int MAX_RUNNING = 10;
		private const int MAX_COUNT_LOAD = 2;
		private EventWatcher mWatcherToCleanStatus;
		private static TimerObject mTimerObject;
		public UploadVoiceService()
		{
			if (this.mWatcherToCleanStatus != null)
			{
				return;
			}
			this.mWatcherToCleanStatus = new EventWatcher(this, null, new EventHandlerDelegate(UploadVoiceService.onHandlerToCleanStatus));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SYNC, this.mWatcherToCleanStatus);
		}
		private static void onHandlerToCleanStatus(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			NetSceneSyncResult netSceneSyncResult = evtArgs.mObject as NetSceneSyncResult;
			if (netSceneSyncResult != null && netSceneSyncResult.syncStatus == SyncStatus.syncEnd && netSceneSyncResult.syncCount == 0)
			{
				UploadVoiceContextLoader.cleanSendingStatus();
				return;
			}
		}
		public bool doSceneBegin(string toUserName)
		{
			if (UploadVoiceRecorder.isRunning())
			{
				Log.smethod_1("UploadVoiceService", "scene cannot begin in recording. ");
				return false;
			}
			string hashString = MD5Core.GetHashString(toUserName + Util.getNowMilliseconds());
			UploadVoiceContext uploadVoiceContext = UploadVoiceContext.createByClientMsgID(hashString);
			if (uploadVoiceContext == null)
			{
				Log.smethod_1("UploadVoiceService", "create upload voice context failed. ");
				return false;
			}
			Log.smethod_4("UploadVoiceService", "cmd to scene begin, toUserName = " + toUserName + " , clientMsgId = " + hashString);
			uploadVoiceContext.mUserName = toUserName;
			uploadVoiceContext.mStatus = 0;
			UploadVoiceContextMgr.getInstance().putToHead(uploadVoiceContext);
			this.checkReadyContextDispatcher();
			UploadVoiceRecorder.start(uploadVoiceContext);
			return true;
		}
		public bool doSceneResume(string clientMsgId)
		{
			Log.smethod_4("UploadVoiceService", "cmd to scene resume,  clientMsgId = " + clientMsgId);
			if (UploadVoiceRecorder.isRunning())
			{
				Log.smethod_1("UploadVoiceService", "scene cannot resume in recording. ");
				return false;
			}
			if (UploadVoiceContextMgr.getInstance().findByClientMsgID(clientMsgId) != null)
			{
				Log.smethod_1("UploadVoiceService", "already running... " + clientMsgId);
				return false;
			}
			if (!UploadVoiceContextLoader.resumeByClientMsgID(clientMsgId))
			{
				return false;
			}
			this.checkReadyContextDispatcher();
			return true;
		}
		public bool doSceneEnd(bool isCancelled = false)
		{
			if (!UploadVoiceRecorder.isRunning())
			{
				Log.smethod_1("UploadVoiceService", "scene cannot end without begin, ignored. ");
				return true;
			}
			UploadVoiceContext currentContext = UploadVoiceRecorder.getCurrentContext();
			if (currentContext == null)
			{
				Log.smethod_1("UploadVoiceService", "scene cannot end with null context, ignored. ");
				return true;
			}
			currentContext.mIsCancelled = isCancelled;
			Log.smethod_4("UploadVoiceService", "cmd to scene end,  clientMsgId = " + currentContext.mClientMsgId);
			UploadVoiceRecorder.stop();
			if (currentContext.isInvalidShortVoice())
			{
				currentContext.mStatus = 4;
				Log.smethod_1("UploadVoiceService", "scene end with invalid short voice. ");
				currentContext.onFinished(3);
				return false;
			}
			return true;
		}
		public static void onVoiceContextDispatcher(object sender, EventArgs e)
		{
			UploadVoiceContextMgr.getInstance().clearnFinishedContext();
			if (UploadVoiceContextMgr.getInstance().getCount() <= 0)
			{
				UploadVoiceContextLoader.checkCleanMsgTrans();
				if (UploadVoiceContextMgr.getInstance().getCount() <= 0)
				{
					UploadVoiceService.closeDispatcherTimer();
					return;
				}
			}
			if (UploadVoiceContextMgr.getInstance().countRunningContext() >= 10)
			{
				return;
			}
			UploadVoiceContext firstContextNeedHandle = UploadVoiceContextMgr.getInstance().getFirstContextNeedHandle();
			if (firstContextNeedHandle == null)
			{
				return;
			}
			Log.smethod_4("UploadVoiceService", "new task startup, clientmsgid = " + firstContextNeedHandle.mClientMsgId);
			NetSceneUploadVoice netSceneUploadVoice = new NetSceneUploadVoice();
			netSceneUploadVoice.mOnSceneFinished += new onSceneFinishedDelegate(UploadVoiceService.onSceneFinished);
			netSceneUploadVoice.doScene(firstContextNeedHandle);
		}
		private static void closeDispatcherTimer()
		{
			Log.smethod_4("UploadVoiceService", "all ready, close dispatcher timer. ");
			UploadVoiceService.mTimerObject.stop();
			UploadVoiceService.mTimerObject = null;
		}
		private static void onSceneFinished(UploadVoiceContext context)
		{
			if (context.mStatus == 5)
			{
				UploadVoiceContextLoader.resetIntervalLimit();
				Log.smethod_4("UploadVoiceService", "ALL completed, clientmsgid = " + context.mClientMsgId);
				return;
			}
			if (context.mStatus == 4)
			{
				Log.smethod_1("UploadVoiceService", "on error, clientmsgid = " + context.mClientMsgId);
				return;
			}
			Log.smethod_1("UploadVoiceService", "status error, clientmsgid = " + context.mClientMsgId);
		}
		private void checkReadyContextDispatcher()
		{
			if (UploadVoiceService.mTimerObject == null)
			{
				Log.smethod_4("UploadVoiceService", "upload voice service dispatcher startup. ");
				UploadVoiceService.mTimerObject = TimerService.addTimer(3, new EventHandler(UploadVoiceService.onVoiceContextDispatcher), 0, -1);
				UploadVoiceService.mTimerObject.start();
			}
		}
		public bool doSceneDirectWithoutRecord(string toUserName, int voiceTimeLength)
		{
			if (voiceTimeLength > 59)
			{
				voiceTimeLength = 59;
			}
			if (voiceTimeLength < 2)
			{
				voiceTimeLength = 2;
			}
			voiceTimeLength *= 1000;
			string hashString = MD5Core.GetHashString(toUserName + Util.getNowMilliseconds());
			UploadVoiceContext uploadVoiceContext = UploadVoiceContext.createByClientMsgID(hashString);
			if (uploadVoiceContext == null)
			{
				Log.smethod_1("UploadVoiceService", "create upload voice context failed. ");
				return false;
			}
			Log.smethod_4("UploadVoiceService", "cmd to scene begin, toUserName = " + toUserName + " , clientMsgId = " + hashString);
			int num = voiceTimeLength * 7 / 10;
			byte[] array = new byte[num];
			for (int i = 0; i < num; i++)
			{
				array[i] = (byte)(i | 1);
			}
			uploadVoiceContext.mCreateTime = (int)(Util.getNowMilliseconds() / 1000.0);
			uploadVoiceContext.mUserName = toUserName;
			uploadVoiceContext.appendOutputData(array, 0, num);
			uploadVoiceContext.mVoiceTimeLength = voiceTimeLength;
			uploadVoiceContext.mEndFlag = 1;
			uploadVoiceContext.mStatus = 0;
			UploadVoiceContextMgr.getInstance().putToTail(uploadVoiceContext);
			this.checkReadyContextDispatcher();
			return true;
		}
		public static void testUploadVoice()
		{
			ServiceCenter.sceneUploadVoice.doSceneBegin("halenwp71");
		}
		public static void testUploadVoiceStop()
		{
			ServiceCenter.sceneUploadVoice.doSceneEnd(false);
		}
	}
}
